package com.sample.library.portlet.book;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletRequest;
import javax.portlet.RenderResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.PortletRequestUtils;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.sample.library.model.Book;
import com.sample.library.model.impl.BookImpl;
import com.sample.library.service.BookLocalServiceUtil;
import com.sample.library.service.BookService;

@RequestMapping(value = "VIEW") 
@Controller(value="addBookController")   
public class AddController {
	
	@Autowired
	@Qualifier("myBookService")
	private BookService bookService;

	@RenderMapping(params="myaction=addBookForm")   
	public String showAddBookForm(RenderResponse response) {                                   
		return "addBookForm";
	}
	
	@ActionMapping(params = "myaction=addBook")
	public void addBook(@ModelAttribute(value="book")Book book) {    
		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
		if( book != null) {
			User _user = themeDisplay.getUser();
			if( _user != null) {
				BookLocalServiceUtil.addBook(_user.getUserId(), book.getTitle());
			}
		}
	}
	
	protected Object formBackingObject(PortletRequest request) throws Exception {
		Book book = null;
		Long bookId = PortletRequestUtils.getLongParameter(request, "bookId");
		if(bookId != null && bookId.longValue() > 0L ) {
			book = (Book)BookLocalServiceUtil.getBook(bookId);
		} else {
			book = new BookImpl();
		}
		return book;
	}
	
	@SuppressWarnings("deprecation")
	 protected void onBind(PortletRequest request, Object command,
	           BindException errors) throws Exception {
		Book preference = (Book)command;
	    super.onBind(request, preference, errors);
	 }
	 
	protected void onSubmitAction(ActionRequest request, ActionResponse response, Object command, BindException errors) throws Exception {
		Book book = (Book)command;
		ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute( WebKeys.THEME_DISPLAY);
		if( book != null) {
			User _user = themeDisplay.getUser();
			if( _user != null) {
				BookLocalServiceUtil.addBook(_user.getUserId(), book.getTitle());
			}
		}
		response.setRenderParameter("action", "viewBook");
	}

	public void handleActionRequest(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
//		String action		= PortletRequestUtils.getStringParameter(actionRequest, "action", "");
//		if( action.equals("editBook")) {
//			String bookIds[] = StringUtil.split( PortletRequestUtils.getStringParameter(actionRequest, "deleteRowIds"));
//			if( bookIds != null && bookIds.length > 0) {
//				List<Book> books = new ArrayList<Book>();
//				for (String orgId : bookIds) {
//					books.add( new Book(orgId));
//				}
//				try {
//					bookService.delete(books);
//				} catch (DataAccessException e) {
//					e.printStackTrace();
//				}
//			}
//			actionResponse.setRenderParameter("action", "viewBook");
//		} else
		super.handleActionRequest(actionRequest, actionResponse);
	}
}
